package cn.itcast.travel.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    private static DataSource ds ;
    static{
        Properties pro = new Properties();
        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
        try {
            pro.load(is);
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            System.err.println("加载配置文件失败: " + e.getMessage());
            throw new RuntimeException(e);
        } catch (Exception e) {
            System.err.println("创建数据源失败: " + e.getMessage());
            throw new RuntimeException(e);
        }
    }
    public static DataSource getDataSource() {
        return ds;
    }
    public  static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}